/**
 * Created by 郝梓淳 on 2017/7/22.
 */
var express = require('express');
var router = express.Router()
var http = require('http')
var quetystring = require('querystring');

function get_massage(post_data,massage,res) {
    var options = {
        host: 'www.tuling123.com',
        port: 80,
        path: '/openapi/api',
        method: 'POST',
        rejectUnauthorized: false,
        headers: {
            "Content-Type": 'application/x-www-form-urlencoded', //这个一定要有
        }
    };

    var req = http.request(options, function (db) {
        console.log('STATUS: ' + res.statusCode);
        console.log('HEADERS: ' + JSON.stringify(db.headers));
        db.setEncoding('utf8');
        db.on('data', function (chunk) {
            console.log('BODY: ' + chunk);
            massage=JSON.parse(chunk).text;
            res.json(massage)
        });
    });
    req.write(post_data);
    req.end();
}

router.post('/',function (req,res) {
    var info = req.body.info;
    var massage = ""
    var post_data = quetystring.stringify({
        key: 'a61c96b097bc4aafa6990c3c62cdc43a',
        info: info
    });
    get_massage(post_data,massage,res);
})

module.exports = router